"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--disk
Case Name   : 主节点备份时，备节点磁盘满
Create At   : 2021/07/26
@zou_jialiang0505328126
Description :
    1.备节点注入磁盘满故障
    2.主节点执行备份指令
    3. 清理故障
    4. 使用备份文件恢复系统
    5.对备库进行build
    6.查询数据是否与备份时一致
Expect      :
    1.成功
    2.失败
    3.成功
    4.成功
    5.成功
    6.主备一致
History     :
"""


import unittest
import os
import time
from yat.test import Node, macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger


class DiskDiskFull(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case063:开始执行---------')
        self.sta1_root_node = Node(node='Standby1Root')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta2_dbuser = Node(node='Standby2DbUser')
        self.com = Common()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.parent_path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.backup_path = os.path.join(self.parent_path, 'base_backup')

        result = self.primary_sh.executDbSql(
            'show wal_sender_timeout;')
        self.log.info(f"wal_sender_timeout is {result}")
        self.wal_sender_timeout = result.strip().splitlines()[-2]

        self.log.info("-----------获取磁盘名 -----------")
        cmd = f"df -h {macro.DB_INSTANCE_PATH}"
        self.log.info(cmd)
        msg = self.sta1_root_node.sh(cmd).result()
        self.log.info(msg)
        self.disk_name = msg.splitlines()[-1].split()[0].strip()

        self.log.info('----创建备份路径----')
        shell_cmd = f'rm -rf {self.backup_path}; mkdir {self.backup_path}'
        self.log.info(shell_cmd)
        result = self.primary_user_node.sh(shell_cmd).result()
        self.log.info(result)

    def test_main(self):
        self.log.info('-----------修改wal_sender_timeout------------------')
        result = self.primary_sh.executeGsguc('reload',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          'wal_sender_timeout=12s')
        self.assertTrue(result)

        self.log.info("--------------1.备节点注入磁盘满故障-------------")
        inject_command = f"rfile_full (diskname) values ({self.disk_name})"
        result = self.com.cfe_inject(self.sta1_root_node, inject_command)
        self.log.info(result)
        self.assertIn("successful execution", result)

        self.log.info("-------------2.主节点执行备份指令---------------")
        result = self.primary_sh.execu_gs_basebackup(
            self.backup_path, self.primary_user_node.db_host,
            self.primary_user_node.db_port)
        self.assertIn(self.constant.BASEBACKUP_SUCCESS_MSG, result)
        result = self.primary_user_node.sh(
            f'ls -l {self.backup_path} | grep "^-" | wc -l').result()
        self.log.info(result)
        self.assertLess(0, int(result))

        self.log.info("--------------3.备节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.disk_name})"
        result = self.com.cfe_clean(self.sta1_root_node, clean_command)
        self.log.info(result)
        self.assertIn("successful execution", result)
        self.log.info("------------重启备节点--------------")
        self.sta_1_sh.startDbInstance("standby")

        self.log.info('----4.使用备份文件恢复数据库----')
        result = self.primary_sh.stopDbCluster()
        self.assertTrue(result)
        shell_cmd = f'rm -rf  {macro.DB_INSTANCE_PATH}/* ;' \
            f'cp -R {self.backup_path}/* {macro.DB_INSTANCE_PATH}'
        self.log.info(shell_cmd)
        result = self.primary_user_node.sh(shell_cmd).result()
        self.log.info(result)
        time.sleep(2)

        self.log.info('----启动集群，启动数据库成功或出现因为备机为Need repair状态的失败----')
        result = self.primary_sh.startDbCluster('get_detail')
        self.log.info(result)
        result = ('Degraded' in result) or \
                 (self.constant.START_SUCCESS_MSG in result)
        self.assertTrue(result)
        time.sleep(2)

        self.log.info('----检查备机是否需要重建----')
        result = self.primary_sh.check_whether_need_build()
        success_msg = self.constant.BUILD_SUCCESS_MSG
        if result:
            result = self.sta_1_sh.executeGsctl('build',
                                                success_msg,
                                                '-b full')
            self.assertTrue(result)
            result = self.sta_2_sh.executeGsctl('build',
                                                success_msg,
                                                '-b full')
            self.assertTrue(result)

        self.log.info('----检查主备是否同步----')
        result = self.sta_1_sh.check_data_consistency()
        self.assertTrue(result)
        result = self.sta_2_sh.check_data_consistency()
        self.assertTrue(result)

        self.log.info('----检查数据一致性----')
        nodes_tuple = (self.primary_user_node,
                       self.sta1_dbuser, self.sta2_dbuser)
        flag = self.com.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = self.com.format_sql_result(
            self.primary_sh.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.com.check_data_sample_by_all(select_sql,
                                                        *nodes_tuple)
            self.assertTrue(flag)

    def tearDown(self):
        self.log.info("-------------清理环境---------------")
        self.log.info("--------------主节点清理磁盘满故障-------------")
        clean_command = f"rfile_full  where(diskname={self.disk_name})"
        result = self.com.cfe_clean(self.sta1_root_node, clean_command)
        self.log.info(result)
        self.log.info("------------重启主节点--------------")
        self.primary_sh.startDbInstance()

        self.log.info('----删除备份文件----')
        shell_cmd = f'rm -rf {self.backup_path}'
        self.log.info(shell_cmd)
        result = self.primary_user_node.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----检查备机是否需要重建----')
        result = self.primary_sh.check_whether_need_build()
        if result:
            self.sta_1_sh.executeGsctl('build',
                                       self.constant.BUILD_SUCCESS_MSG,
                                       '-b full')
            self.sta_2_sh.executeGsctl('build',
                                       self.constant.BUILD_SUCCESS_MSG,
                                       '-b full')

        self.log.info('-----------还原wal_sender_timeout------------------')
        result = self.primary_sh.executeGsguc('reload',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'wal_sender_timeout='
                                          f'{self.wal_sender_timeout}')
        self.log.info(result)
        self.log.info(
            '--------Opengauss_Reliability_Hardware_Case063:执行完成--------')
